<template>
  <div>
    <div class="advertisement-info">
      <div class="content">
        <div class="project-info">
          <div class="advertisement-form mt50">
            <el-form
              style="max-width: 1200px"
              :model="state.form"
              label-position="top"
              ref="formRef"
              :rules="rules"
            >
              <el-form-item label="昵称" prop="nicheng">
                <el-col :span="10">
                  <el-input size="large" v-model="state.form.nicheng" />
                </el-col>
              </el-form-item>
              <el-form-item label="头像" prop="touxiang">
                <bjz-oss
                  :previewList="state.form.touxiang"
                  :accept="['.jpg', '.png', '.jpeg']"
                  @success="successImg"
                  @remove="removeImg"
                />
              </el-form-item>
              <el-form-item class="button-group">
                <el-button
                  style="margin-left: 100px"
                  type="primary"
                  @click="submitForm(formRef)"
                  >提交</el-button
                >
                <el-button
                  type="primary"
                  style="margin-left: 50px"
                  @click="$router.go(-1)"
                  >取消</el-button
                >
              </el-form-item>
            </el-form>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script setup>
import { reactive, ref } from "vue";
import { useRoute, useRouter } from "vue-router";
import { baseMsg } from "@/utils/utils";
import {
  EditVirtualUser,
  AddVirtualUser,
  GetVirtualUser,
} from "@/api/virtualUser";
const route = useRoute();
const router = useRouter();
const formRef = ref(null);
const validateImg = (rule, value, callback) => {
  if (state.form.touxiang && state.form.touxiang.length) {
    callback();
  } else {
    callback(new Error("请上用户头像"));
  }
};
const rules = {
  nicheng: [{ required: true, trigger: "blur", message: "请输入用户昵称" }],
  touxiang: [{ required: true, trigger: "blur", validator: validateImg }],
};
const state = reactive({
  form: {
    nicheng: "",
    touxiang: [],
  },
});
const init = () => {
  if (route.query.type === "edit") {
    GetVirtualUser({ id: route.query.id }).then((res) => {
      if (res.code == 4201) {
        state.form = res.data;
      }
    });
  }
};
// 图片上传成功
const successImg = (res) => {
  state.form.touxiang.push(res);
};
// 上传文件删除
const removeImg = (index) => {
  state.form.touxiang.splice(index, 1);
};
// 提交表单
const submitForm = async (formEl) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      if (route.query.type == "add") {
        AddVirtualUser(state.form).then((res) => {
          if (res.code == 4202) {
            baseMsg("新增成功");
            router.go(-1);
          }
        });
      }
      if (route.query.type == "edit") {
        EditVirtualUser(state.form).then((res) => {
          if (res.code == 4202) {
            baseMsg("编辑成功");
            router.go(-1);
          }
        });
      }
    }
  });
};
init();
</script>
<style lang="scss" scoped>
.content {
  border: none !important;
}
.project-info {
  padding: 30px 100px;
  font-size: 20px;
  color: black;
  display: flex;
  .advertisement-form {
    width: 1200px;
  }
}
.el-form-item {
  display: flex;
  flex-direction: column;
  &.vue-editor {
    align-items: baseline;
  }
  &.status {
    flex-direction: row;
  }
}
</style>
<style lang="scss">
.button-group {
  margin-top: 50px;
  .el-form-item__content {
    width: 100% !important;
    justify-content: center;
  }
}
</style>
